
<h1><span class="yiyi-st" id="yiyi-12">numpy.core.records.fromfile</span></h1>
        <blockquote>
        <p>原文：<a href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.core.records.fromfile.html">https://docs.scipy.org/doc/numpy/reference/generated/numpy.core.records.fromfile.html</a></p>
        <p>译者：<a href="https://github.com/wizardforcel">飞龙</a> <a href="http://usyiyi.cn/">UsyiyiCN</a></p>
        <p>校对：（虚位以待）</p>
        </blockquote>
    
<dl class="function">
<dt id="numpy.core.records.fromfile"><span class="yiyi-st" id="yiyi-13"> <code class="descclassname">numpy.core.records.</code><code class="descname">fromfile</code><span class="sig-paren">(</span><em>fd</em>, <em>dtype=None</em>, <em>shape=None</em>, <em>offset=0</em>, <em>formats=None</em>, <em>names=None</em>, <em>titles=None</em>, <em>aligned=False</em>, <em>byteorder=None</em><span class="sig-paren">)</span><a class="reference external" href="http://github.com/numpy/numpy/blob/v1.11.3/numpy/core/records.py#L721-L787"><span class="viewcode-link">[source]</span></a></span></dt>
<dd><p><span class="yiyi-st" id="yiyi-14">从二进制文件数据创建数组</span></p>
<p><span class="yiyi-st" id="yiyi-15">如果file是一个字符串，那么该文件被打开，否则假定它是一个文件对象。</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">tempfile</span> <span class="k">import</span> <span class="n">TemporaryFile</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">empty</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span><span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;f8,i4,a5&apos;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">[</span><span class="mi">5</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="mf">0.5</span><span class="p">,</span><span class="mi">10</span><span class="p">,</span><span class="s1">&apos;abcde&apos;</span><span class="p">)</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fd</span><span class="o">=</span><span class="n">TemporaryFile</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">newbyteorder</span><span class="p">(</span><span class="s1">&apos;&lt;&apos;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">tofile</span><span class="p">(</span><span class="n">fd</span><span class="p">)</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fd</span><span class="o">.</span><span class="n">seek</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">core</span><span class="o">.</span><span class="n">records</span><span class="o">.</span><span class="n">fromfile</span><span class="p">(</span><span class="n">fd</span><span class="p">,</span> <span class="n">formats</span><span class="o">=</span><span class="s1">&apos;f8,i4,a5&apos;</span><span class="p">,</span> <span class="n">shape</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span>
<span class="gp">... </span><span class="n">byteorder</span><span class="o">=</span><span class="s1">&apos;&lt;&apos;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">r</span><span class="p">[</span><span class="mi">5</span><span class="p">])</span>
<span class="go">(0.5, 10, &apos;abcde&apos;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">shape</span>
<span class="go">(10,)</span>
</pre></div>
</div>
</dd></dl>
